package com.vilinno.tlias.controller;

import com.vilinno.tlias.pojo.Student;
import com.vilinno.tlias.pojo.StudentQueryParam;
import com.vilinno.tlias.pojo.PageResult;
import com.vilinno.tlias.pojo.Result;
import com.vilinno.tlias.service.StudentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/students")
public class StudentController {

    private static final Logger log = LoggerFactory.getLogger(com.vilinno.tlias.controller.StudentController.class);

    @Autowired
    private StudentService studentService;

    @GetMapping
    public Result page(StudentQueryParam studentQueryParam) {

        log.info("分页查询：{}", studentQueryParam);

        PageResult<Student> pageResult = studentService.page(studentQueryParam);

        return Result.success(pageResult);

    }

    @PostMapping
    public Result add(@RequestBody Student student) {

        log.info("新增员工： {}", student);

        studentService.save(student);

        return Result.success();

    }

    /**
     * 删除员工信息（一个员工和多个员工）
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable("ids") List<Integer> ids) {

        log.info("删除员工信息为：{}", ids);

        studentService.delete(ids);

        return Result.success();

    }

    /**
     * 根据id查询员工信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id) {

        log.info("根据员工id查询信息: {}", id);
        Student student = studentService.getInfo(id);
        return Result.success(student);
    }

    /**
     * 修改员工信息
     *
     * @param student
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Student student) {
        log.info("修改员工信息: {}", student);
        studentService.update(student);
        return Result.success();
    }

    @PutMapping("/violation/{id}/{score}")
    public Result violation(@PathVariable Integer id, @PathVariable Integer score) {
        log.info("修改学生违纪情况：给主键为{}的同学，扣{}分", id, score);
        studentService.updateViolation(id,score);
        return Result.success();
    }
}

